//
//  main.m
//  OC _ 数组字典复习
//
//  Created by dllo on 15/12/7.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Fraction.h"
#import "AddressBook.h"
#import "Contact.h"
#import "AddressBookV2.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
     
        
        
# pragma mark - 复习1: 分数
#if 0
        /* 创建对象 */
        Fraction *fr1 = [[Fraction alloc]initWithNum:1 deno:3];
        
        Fraction *fr2 = [[Fraction alloc]initWithNum:2 deno:5];
        
        
        /*使用对象*/
        [fr2 addFraction:fr1];
        
        Fraction *addFra = [fr2 addFraction:fr1];
        
        NSLog(@"%ld/%ld",addFra.num,addFra.deno);

        
# pragma mark - 复习2: 联系人
        
        /*涉及到两个类  分别是AddressBook(通讯录类) Contact(联系人类)*/
        
        /*创建一个通讯录对象*/
        AddressBook *ab = [[AddressBook alloc]init];
        /*创建两个联系人对象*/
        Contact *con1 = [[Contact alloc]initWithName:@"zhangsan" phonenumber:@"123456" address:@"dalian"];
        Contact *con2 = [[Contact alloc]initWithName:@"lisi" phonenumber:nil address:@"beijing"];
        
        /*使用通讯录*/
        [ab addContact:con1];
        [ab addContact:con2];
        /* 显示所有联系人 */
        [ab listAllContacts];
        /*删除指定联系人*/
        [ab deleteContact:con1];
        
        /* 显示所有联系人 */
        [ab listAllContacts];
#endif
        
        
        
#pragma mark - 复习 通讯录(中等难度)
        
        Contact *con1 = [[Contact alloc]initWithName:@"zhangsan" phonenumber:@"123456" address:@"dalian"];
        Contact *con2 = [[Contact alloc]initWithName:@"lisi" phonenumber:nil address:@"beijing"];
       
        
        /*创建通讯录对象*/
        AddressBookV2 *abV2 = [[AddressBookV2 alloc]init];
        /*对象方法*/
        [abV2 addContact:con1];
        [abV2 addContact:con2];
        [abV2 listAllContact];
        /*显示某个分组下得所有联系人*/
        
        [abV2 listForGroup:@"z"];
        
        
#pragma mark - 练习: 省市区
        
        
        NSString *file = [NSString stringWithContentsOfFile:@"/Users/dllo/Desktop/码农的四个月/OC/OC _ 数组字典复习/OC _ 数组字典复习/area.txt" encoding:NSUTF8StringEncoding error:nil];
        
        NSLog(@"%@",file);
        
        /*将字符串进行截取,按照\n*/
        NSArray *arr = [file componentsSeparatedByString:@"\n"];
        
        NSString *provinceName = nil;
        NSString *cityName = nil;
        NSString *areaName = nil;
        /** 创建中国数组*/
        NSMutableArray *chinaArr = [NSMutableArray array];
        
        /* 遍历数组 */
        for (NSString *line in arr) {
            if (![line hasPrefix:@" "]) {
                /** 字符串不是以空格开头 说明是省名*/
                provinceName = line;/** 记录省名*/
                /** 创建省字典加入到中国数组中*/
                NSMutableArray *cityArr = [NSMutableArray array];
                NSMutableDictionary *proDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:provinceName,@"省名",cityArr,@"市", nil];
                [chinaArr addObject:proDic];
                
            }else if (![line hasPrefix:@"    "]) {
                /** 说明是市名*/
                cityName = line;/** 记录市名*/
                /** 创建市字典  添加在对应省的市数组中*/
                NSMutableArray *areaArr= [NSMutableArray array];
                
                NSMutableDictionary *cityDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:cityName,@"市名",areaArr,@"区", nil];
                
                
                /** 从中国数组里找出对应省的市数组*/
                
                NSMutableArray *tempArr = [[chinaArr lastObject] objectForKey:@"市"];
                [tempArr addObject:cityDic];
                
                
            } else {
                
                areaName = line;/** 记录下区名*/
                /*找出区所在省得市数组的相应市的区*/
                NSMutableArray *tempArr = [[chinaArr lastObject] objectForKey:@"市"];
                NSMutableArray *tempAreas = [[tempArr lastObject]objectForKey:@"区"];
                [tempAreas addObject:areaName];
            }
            
        }
        
    }
    return 0;
}
